package com.wolfalpha.lwtool.activity;

import com.wolfalpha.lwtool.R;
import com.wolfalpha.lwtool.constant.Application;
import com.wolfalpha.lwtool.service.MyService;
import com.wolfalpha.lwtool.service.impl.UserServiceImpl;
import com.wolfalpha.lwtool.vo.CountInfo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CountActivity extends Activity {
	private ProgressDialog pd;
	private EditText mobile;
	private EditText email;
	private UserServiceImpl sav;
	private CountInfo cf = null;
	private TextView yes_company;
	private TextView yes_parttimer;
	private TextView yes_user;
	private TextView lastw_company;
	private TextView lastw_parrtimer;
	private TextView lastw_user;
	private TextView parttimers;
	private TextView jobs;
	private TextView companys;
	private TextView unau_company;
	private TextView unau_job;
	private Button auJob;// 审核兼职按钮
	private Button auCom;// 审核企业按钮
	private CountTask ct = null;
	private boolean logined = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sav = new UserServiceImpl();
		setContentView(R.layout.activity_count);
		initView();
		initAccess();
		initListeners();
	}

	private void initListeners() {
		auJob.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (cf != null) {
					if (cf.getUnAudited_job() > 0) {
						Intent intent = new Intent();
						intent.setClass(CountActivity.this,
								AuditeJobActivity.class);
						startActivity(intent);
					} else {
						Toast.makeText(CountActivity.this, "没有未认证的兼职",
								Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		auCom.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (cf != null) {
					if (cf.getUnAudited_company() > 0) {
						Intent intent = new Intent();
						intent.setClass(CountActivity.this,
								AuditeComActivity.class);
						startActivity(intent);
					} else {
						Toast.makeText(CountActivity.this, "没有未认证的企业",
								Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
	}

	private void initView() {
		yes_company = (TextView) findViewById(R.id.yes_company);
		yes_parttimer = (TextView) findViewById(R.id.yes_parttimer);
		yes_user = (TextView) findViewById(R.id.yes_user);
		lastw_company = (TextView) findViewById(R.id.lastw_company);
		lastw_parrtimer = (TextView) findViewById(R.id.lastw_parrtimer);
		lastw_user = (TextView) findViewById(R.id.lastw_user);
		parttimers = (TextView) findViewById(R.id.parttimers);
		companys = (TextView) findViewById(R.id.companys);
		jobs = (TextView) findViewById(R.id.jobs);
		unau_company = (TextView) findViewById(R.id.unau_company);
		unau_job = (TextView) findViewById(R.id.unau_job);

		auJob = (Button) findViewById(R.id.audite_job);
		auCom = (Button) findViewById(R.id.audite_company);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (logined) {
			ct = new CountTask();
			ct.execute();
		}
	}

	/**
	 * 登录框
	 */
	public void initAccess() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				CountActivity.this);
		builder.setCancelable(false);
		builder.setTitle("请登录管理员帐号");
		View layout = LayoutInflater.from(CountActivity.this).inflate(
				R.layout.dialog_input, null);

		mobile = (EditText) layout.findViewById(R.id.d_mobile);
		email = (EditText) layout.findViewById(R.id.d_email);

		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(CountActivity.this);
		if (sp.contains("mobile")) {
			mobile.setText(sp.getString("mobile", ""));
			email.setText(sp.getString("email", ""));
		}

		builder.setView(layout);
		builder.setPositiveButton("登录", new OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				LoginTask lt = new LoginTask();
				lt.execute();
				ct = new CountTask();
				ct.execute();
			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				finish();
			}
		});
		builder.create();
		builder.show();
	}

	private class LoginTask extends AsyncTask<String, Integer, String> {

		// onPreExecute方法用于在执行后台任务前做一些UI操作
		@Override
		protected void onPreExecute() {
			if (pd == null)
				pd = new ProgressDialog(CountActivity.this);
			pd.setMessage("正在验证并登录");
			pd.setCancelable(false);
			pd.show();
		}

		// doInBackground方法内部执行后台任务,不可在此方法内修改UI
		@SuppressLint("CommitPrefEdits")
		@Override
		protected String doInBackground(String... params) {
			try {
				SharedPreferences sp = PreferenceManager
						.getDefaultSharedPreferences(CountActivity.this);
				Editor ed = sp.edit();
				ed.putString("mobile", mobile.getText().toString());
				ed.putString("email", email.getText().toString());
				ed.commit();
				boolean result = sav.adminLogin(mobile.getText().toString(),
						email.getText().toString());
				if (!result) {
					System.out.println("Login Fail");
					this.cancel(true);
				}
				logined = true;
			//	if (!Application.serviceStarted)
				{
					Intent service = new Intent(CountActivity.this,
							MyService.class);
					startService(service);
				}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("Login Fail");
				this.cancel(true);
			}
			return null;
		}

		// onProgressUpdate方法用于更新进度信息
		@Override
		protected void onProgressUpdate(Integer... progresses) {
		}

		// onPostExecute方法用于在执行完后台任务后更新UI,显示结果
		@Override
		protected void onPostExecute(String result) {
			pd.dismiss();
		}

		// onCancelled方法用于在取消执行中的任务时更改UI
		@Override
		protected void onCancelled() {
			Toast.makeText(CountActivity.this, "登录失败", Toast.LENGTH_SHORT)
					.show();
			pd.dismiss();
			CountActivity.this.finish();
		}
	}

	private class CountTask extends AsyncTask<String, Integer, String> {

		// onPreExecute方法用于在执行后台任务前做一些UI操作
		@Override
		protected void onPreExecute() {
			if (pd == null)
				pd = new ProgressDialog(CountActivity.this);
			pd.setMessage("正在刷新");
			pd.setCancelable(false);
			pd.show();
		}

		// doInBackground方法内部执行后台任务,不可在此方法内修改UI
		@SuppressLint("CommitPrefEdits")
		@Override
		protected String doInBackground(String... params) {
			try {
				cf = sav.adminGetCount();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "";
		}

		// onProgressUpdate方法用于更新进度信息
		@Override
		protected void onProgressUpdate(Integer... progresses) {
		}

		// onPostExecute方法用于在执行完后台任务后更新UI,显示结果
		@Override
		protected void onPostExecute(String result) {
			if (cf != null) {
				yes_company.setText(String.valueOf(cf.getYesterday_company()));
				yes_parttimer.setText(String.valueOf(cf
						.getYesterday_parttimer()));
				yes_user.setText(String.valueOf(cf.getYesterday_user()));
				lastw_company.setText(String.valueOf(cf.getLastWeek_company()));
				lastw_parrtimer.setText(String.valueOf(cf
						.getLastWeek_parttimer()));
				lastw_user.setText(String.valueOf(cf.getLastWeek_user()));
				parttimers.setText(String.valueOf(cf.getParttimer()));
				companys.setText(String.valueOf(cf.getCompany()));
				jobs.setText(String.valueOf(cf.getJob()));
				unau_company.setText(String.valueOf(cf.getUnAudited_company()));
				unau_job.setText(String.valueOf(cf.getUnAudited_job()));
			}
			this.cancel(true);
			pd.dismiss();
		}

		// onCancelled方法用于在取消执行中的任务时更改UI
		@Override
		protected void onCancelled() {
			Toast.makeText(CountActivity.this, "更新失败,请稍后重试", Toast.LENGTH_SHORT)
					.show();
			pd.dismiss();
		}
	}
}
